<template>
  <div>
    <el-dialog :visible="showDialog" :title="formData.id?'编辑页面':'添加页面'" top="10vh" :close-on-click-modal="false" @close="btnCancel">
      <el-form ref="formData" label-position="top" label-width="80px" :model="formData" :rules="rules">
        <el-form-item label="名称" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content" class="content" style="height:200px">
          <VueEditor v-model="formData.content" style="height:100px" />

        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end" style="margin-top:80px">
        <el-button type="primary" @click="btnOK">确定</el-button>
        <el-button type="danger" @click="btnCancel">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { publisherPages, getPage, editPage } from '@/api/page'
import { VueEditor } from 'vue2-editor'
// Advanced Use - Hook into Quill's API for Custom Functionality
export default {
  components: {
    VueEditor
  },
  props: {
    showDialog: {
      requied: true,
      type: Boolean
    }
  },
  data() {
    // 文本校验
    return {
      formData: {
        title: '',
        content: ''
      },
      confirmStatus: true,
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    btnCancel() {
      // 将数据全部清空
      this.$refs.formData.resetFields()
      this.formData = {}
      this.$emit('update:showDialog', false)
    },
    async btnOK() {
      // 触发文本框的编辑
      await this.$refs.formData.validate()
      if (!this.confirmStatus) {
        this.$message.error('请等待')
        return
      }
      this.confirmStatus = false
      if (this.formData.id) {
        await editPage(this.formData)
      } else {
        await publisherPages(this.formData)
      }

      this.$message.success('操作页面成功')
      this.$emit('initPages')
      // 将数据全部清空
      this.$refs.formData.resetFields()
      this.$emit('update:showDialog', false)
      this.$emit('initpageLength')
      this.confirmStatus = true
    },
    // 获取数回显
    async getPage(id) {
      const res = await getPage(id)
      console.log(res)
      this.formData = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .VueEditor{
  height: 100px;
  .ql-container .ql-snow{
    height: 100px;
  }
}
::v-deep .ql-editor{
  min-height: 100px;
}
.content{

  ::v-deep .el-form-item__error{
    margin-top: 100px;
  }
}
</style>
